<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Feiyongkemu3 extends Common
{

	//框架
    public function index()
    {

        $get = Request::instance()->get();
        $this->assign('get',$get);
        $where = [];
        if(isset($get['sn']))
            $where['sn'] = ['like','%'.$get['sn'].'%'];
        if(isset($get['title']))
            $where['title'] = ['like','%'.$get['title'].'%'];
        if(isset($get['fifeiyongkemu']))
            $where['fifeiyongkemu'] = ['exp',"in(select id from cg_fifeiyongkemu where title like '%".$get['fifeiyongkemu']."%')"];
        if(isset($get['esuseway'])&&$get['esuseway']!='请选择')
            $where['esuseway'] = ['like','%"'.$get['esuseway'].'"%'];
        if(isset($get['channeltypes'])&&$get['channeltypes']!='请选择')
            $where['channeltypes'] = ['like','%"'.$get['channeltypes'].'"%'];
        if(isset($get['pid']))
            $where['pid'] = ['exp',"in(select id from cg_feiyongkemu2 where title like '%".$get['pid']."%')"];

        $count = Db::table('cg_feiyongkemu3')->where($where)->count();
        $data = Db::table('cg_feiyongkemu3')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_feiyongkemu3')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['pid_title'] = Db::table('cg_feiyongkemu2')->where(array('id'=>$v['pid']))->value('title');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]['esuseway'] = unserialize($v['esuseway']);
            
            $data[$k]['fifeiyongkemu'] = Db::table('cg_fifeiyongkemu')->where(array('id'=>$v['fifeiyongkemu']))->value('title');
            if($data[$k]['channeltypes'] ){
                 $data[$k]['channeltypes'] = unserialize($v['channeltypes']);
                foreach($data[$k]['channeltypes'] as $k1=>$v1){
                    $data[$k]['channeltypess'][] = Db::table('cg_channel_types1')->where(array('id'=>$v1))->value('title');
                }
                $data[$k]['channeltypess'] = implode(',',$data[$k]['channeltypess']);
            }else{
                $data[$k]['channeltypess'] ='';
            }
           
            foreach($data[$k]['esuseway'] as $k1=>$v1){
                $data[$k]['esuseways'][] = Db::table('cg_esuseway')->where(array('id'=>$v1))->value('title');
            }
            $data[$k]['esuseways'] = implode(',',$data[$k]['esuseways']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
		$this->assign('maxpage',$maxpage);
		$this->assign('count',$count);
        //查询所有二级科目
        $yiji = Db::table('cg_feiyongkemu2')->select();
        $this->assign('yiji',$yiji);

        //查询费用科目使用途径
        $way = Db::table('cg_esuseway')->where(array('pid'=>0))->select();
        foreach($way as $k=>$v){
            $child = Db::table('cg_esuseway')->where(array('pid'=>$v['id']))->select();
            if($child)
                $way[$k]['_child'] = $child;
        }
        $this->assign('esuseway',$way);

        //查询财务费用科目
        $fifeiyongkemu = Db::table('cg_fifeiyongkemu')->select();
        $this->assign('fifeiyongkemu',$fifeiyongkemu);

        //查询一级渠道类型
        $channel_types1 = Db::table('cg_channel_types1')->select();
        $this->assign('channel_types1',$channel_types1);
        //费用科目类型
        $feitype=db("cg_fei_type")->select();
        $this->assign("feitype",$feitype);
        //费用支付方式
        $pay=db("cg_paymethod")->select();
        $this->assign("pay",$pay);
        return $this->fetch('index');
    }

    public function add(){

        $validate = new Validate([
            'title|科目名称'  => 'require|max:100',
            'sn|科目编号' => 'require|max:15',
            'pid|所属二级科目' => 'require',
            'esuseway|费用科目使用途径' => 'require',
            'fifeiyongkemu|对应财务费用科目' => 'require',
            'channeltypes|费用对应渠道' => 'require',
            
        ]);

        $request = Request::instance();
        $data = $request->post();
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $sncnt = Db::table('cg_feiyongkemu3')->where('pid =\''.$data['pid'].'\' and sn = \''.$data['sn'].'\'')->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'科目编号已存在'
            ]);die;
        }
        ;

        $data['esuseway'] = serialize($data['esuseway']);
        if(isset($data['channeltypes']))
            $data['channeltypes'] = serialize($data['channeltypes']);
        else
            $data['channeltypes']=null;
        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        if(isset($data["payway"])){
            $data['payway'] = serialize($data['payway']);
        }else{
            $data['payway'] ="";
        }
        $bool = Db::table('cg_feiyongkemu3')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    public function updateview(){
        //查询所有二级科目
        $yiji = Db::table('cg_feiyongkemu2')->select();
        $this->assign('yiji',$yiji);

        //查询费用科目使用途径
        $way = Db::table('cg_esuseway')->where(array('pid'=>0))->select();
        foreach($way as $k=>$v){
            $child = Db::table('cg_esuseway')->where(array('pid'=>$v['id']))->select();
            if($child)
                $way[$k]['_child'] = $child;
        }
        $this->assign('esuseway',$way);

        //查询财务费用科目
        $fifeiyongkemu = Db::table('cg_fifeiyongkemu')->select();
        $this->assign('fifeiyongkemu',$fifeiyongkemu);

        //查询一级渠道类型
        $channel_types1 = Db::table('cg_channel_types1')->select();
        $this->assign('channel_types1',$channel_types1);

        $data = Db::table('cg_feiyongkemu3')->where(array('id'=>$this->request->get('id')))->find();
        $data['esuseway'] = unserialize($data['esuseway']);
        $data['channeltypes'] = unserialize($data['channeltypes']);
        if($data["payway"]){
            $data['payway'] = unserialize($data['payway']);
        }else{
            $data['payway'] =[];
        }
        //print_r($data['payway']);
        $this->assign('data',$data);
        //费用科目类型
        $feitype=db("cg_fei_type")->select();
        $this->assign("feitype",$feitype);
        //费用支付方式
        $pay=db("cg_paymethod")->select();
        $this->assign("pay",$pay);

        return $this->fetch('updateview');
    }

    public function update(){
        $request = Request::instance();
        $data = $request->post();
         $validate = new Validate([
            'title|科目名称'  => 'require|max:100',
            'sn|科目编号' => 'require|max:15',
            'pid|所属二级科目' => 'require',
            'esuseway|费用科目使用途径' => 'require',
            'fifeiyongkemu|对应财务费用科目' => 'require',
            'channeltypes|费用对应渠道' => 'require',
            
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }
        $sncnt = Db::table('cg_feiyongkemu3')->where('pid =\''.$data['pid'].'\' and id !=\''.$data['id'].'\' and sn = \''.$data['sn'].'\'')->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'科目编号已存在'
            ]);die;
        }

        $data['esuseway'] = serialize($data['esuseway']);
        $data['channeltypes'] = serialize($data['channeltypes']);
        if(isset($data["payway"])){
            $data['payway'] = serialize($data['payway']);
        }else{
            $data['payway'] ="";
        }
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();
        $bool = Db::table('cg_feiyongkemu3')->where(array('id'=>$data['id']))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_feiyongkemu3')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }
}
